package MaterialPack;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Van persie
 */
import Supporter.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.IOException;
import javax.microedition.lcdui.game.Sprite;

public class Animation {

  /** 
   * Location to draw the animation, set these fields to
   * change the location where the image is drawn.
   */
  int x, y;

  /**
   * The width and the height of the images (max of all if they
   * are different).
   * They are set when images are loaded and should not be changed.
   */
  int width, height;

  /**
   * Vector of images in the sequence.
   */
  private Vector images;

  /**
   * Current index into the sequence of images.
   */
  private int index;

  /**
   * Size of sequence of images.
   * Set to a large number until the last image of
   * the sequence has been read.
   */
  private int size;
  /**
   * Prefix or name of the image.
   */
  //private String prefix;
  
  private boolean warp = true;

  /**
   * Create a new Animation.
   */

  public  Animation(){}
  public Animation(int xAnch,int yAnch, String Path) {
      x = xAnch;
      y = yAnch;
      warp = true;        
      Load(Path) ;
      //images = new Vector(30);
  }
  

  /**
   * Advance to the next image.
   * If the number of images is known then just advance
   * and wrap around if necessary.
   * If the number of images is not known then when 
   * advancing off the end of the known images try to
   * create a new image using the pattern.
   * When an attempt fails that sets the number of images.
   */
  public  void next() {
      //int nextindex;
      index = (index + 1);
      if(index == getImages().size())
          if(warp == true)
              index = 0;
      
  }        
   

  /**
   * Paint the current image in the sequence.
   * The image is drawn to the target graphics context
   * at the x, and y of the Animation.
   * @param g graphics context to which the next image is drawn.
   */
  public void paint(Graphics g) {
    if (getImages().isEmpty() != true) {
      g.drawImage((Image)getImages().elementAt(index), x, y, Graphics.HCENTER | Graphics.VCENTER);
    }
        
  }

 
  public void Load(String pathImg)  {
      // sprites info
      int nCols = 7;
      int nRows = 1;
      
      images = new Vector();
      
    
    index = 0;
    Image image;
//    try {
        //image = Image.createImage(pathImg); 
//        Image tempPNG = Image.createImage(pathImg); 
//        width = tempPNG.getWidth();
//        height = tempPNG.getHeight();
//        int rgbData[] = new int[width*height] ;
//        tempPNG.getRGB(rgbData, 0, width, 0, 0, width, height);
//        
//        int colorkey = rgbData[1];
//        for (int i = 0; i < rgbData.length; i++) { 
//            //set the image to full transparency 
//            if(rgbData[i] == colorkey)
//                rgbData[i] &= 0x00ffffff; 
//} 
//        
//        image = Image.createRGBImage(rgbData, width, height, true);
//        
        image = ImageSupporter.transparent(pathImg);
        //tempPNG = null;
        
        height = image.getHeight();
        width = image.getWidth();
        int wStep = width/nCols;
        int hStep = height/nRows;
        Image temp;
        
        for(int i = 0; i < nRows; i++)
            for(int j = 0; j < nCols;j++)
            {
                temp = Image.createImage(image, j*wStep, i*hStep, wStep, hStep, Sprite.TRANS_NONE);
                getImages().addElement(temp);
            }
//    } catch (IOException ex) {
//      ex.getMessage();
//    } 
  }

  /**
   * Reset the Animation to reduce memory usage.
   * Discard all but the first image.
   */
  void reset() {
    if (getImages().size() > 0) {
      for (int i = 0; i < getImages().size(); i++)
            getImages().setElementAt(null, i);
    }
  }

    /**
     * @return the images
     */
    public Vector getImages() {
        return images;
    }

    /**
     * @param images the images to set
     */
    public void setImages(Vector images) {
        this.images = images;
    }
}
